<!-- 点击编辑弹出的dialog -->
<template>
    <el-dialog
            :visible.sync="visible"
            :modal-append-to-body='false'
            :close-on-click-modal='false'
            @close="close"
           >
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="求职信息aid" prop="aid" v-show="false">
                    <el-input type="hidden" v-model="ruleForm.aid" v-show="false"></el-input>
                </el-form-item>
                <el-form-item v-if="isAdmin === true" label="谁找工作" prop="huntJobUser">
                    <el-input prefix-icon="el-icon-user" v-model="ruleForm.huntJobUser" placeholder="请输入用户" clearable></el-input>
                </el-form-item>
                <el-form-item label="找什么工作" prop="jobName">
                    <el-input prefix-icon="el-icon-postcard" v-model="ruleForm.jobName" placeholder="请输入求职岗位" clearable></el-input>
                </el-form-item>
                <el-form-item label="职位的类型" prop="jobType">
                     <el-select v-model="ruleForm.jobType" placeholder="请选择职位的类型">
                        <el-option label="全职" value="全职"></el-option>
                        <el-option label="实习" value="实习"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="薪资" prop="salary">
                    <el-input prefix-icon="el-icon-money" v-model="ruleForm.salary" placeholder="money要多少" clearable></el-input>
                </el-form-item>
                <el-form-item label="几点上班" prop="workTime">
                    <el-time-picker
                        v-model="ruleForm.workTime"
                        :picker-options="{
                            selectableRange: '00:00:00 - 23:59:59'
                        }"
                        placeholder="请选择上班时间"
                    >
                    </el-time-picker>
                </el-form-item>
                <el-form-item label="期望工作地" prop="workAddress">
                    <el-input prefix-icon="el-icon-money" v-model="ruleForm.workAddress" placeholder="期望工作地点" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
    </el-dialog>
</template>

<script>
import { checkHunterJobUserIsExist,isAdminApi,editOrAddJobInfoApi } from "../../../api/jobInfoManageApi/index";
import { getStore } from '../../../storage/storage';
export default { 
    name: 'jobinfo-addoredit',
    data () {
        var checkHuntJobUser = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('请输入正确的用户姓名'))
            }
            setTimeout(() => {
                const params = {
                    huntJobUser: value,
                }
                checkHunterJobUserIsExist(params).then(res=>{
                    var flag = res.data.data;
                    if(flag === false){
                        return callback(new Error('不存在这个用户，请去用户管理添加用户或者重新输入！'));
                    }else{
                        return callback();
                    }
                })
            }, 100)
        }
        return {
            isAdmin: true, // 是否是管理员
            visible: this.show,
            ruleForm: {
                aid: -1, // 求职信息aid
                huntJobUser: '',
                jobName: '',
                workTime: '',
                salary: '',
                workAddress: '', // 期望工作地点
                jobType: '', // 全职还是实习
            },
            rules: {
                huntJobUser: [
                    { required: true, message: '请输入正确的求职者名称', trigger: 'blur' },
                    { validator: checkHuntJobUser, trigger: 'blur' },
                ],
                jobName: [
                    { required: true, message: '请输入岗位名称', trigger: 'blur' }    
                ],
                salary: [
                    { required: true, message: '请输入薪资', trigger: 'blur' }
                ],
                workAddress: [
                    { required: true, message: '请输入期望的工作地点', trigger: 'blur' },
                ],
                jobType: [
                    { required: true, message: '请选择工作类型', trigger: 'change' },
                ],
                workTime: [
                    { required: true, message: '请选择上班时间', trigger: 'change' },
                ],
            }
        };
    },
    props: {
        show: {
            type: Boolean,
            default: false
        },
        detailRow: {
            type: Object,
            require: true,
        }
    },
    watch: {
        show () {
            this.visible = this.show;
        },
        detailRow() {
            this.ruleForm.aid = this.detailRow.aid;
            this.ruleForm.huntJobUser = this.detailRow.huntUser;
            this.ruleForm.jobName = this.detailRow.posName;
            this.ruleForm.salary = this.detailRow.salary;
            this.ruleForm.workAddress = this.detailRow.workAddress;
            this.ruleForm.jobType = this.detailRow.jobType;
            if (this.ruleForm.aid !== -1) {
                this.ruleForm.workTime = new Date(2022,2,13,9);
            }
        },
    },
    methods: {

      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = {
                aid: this.ruleForm.aid,
                account: getStore("account"),
                huntJobUser: this.ruleForm.huntJobUser ? this.ruleForm.huntJobUser : "求职用户就是你自己",
                jobName: this.ruleForm.jobName,
                salary: this.ruleForm.salary,
                workTime: this.ruleForm.workTime,
                workAddress: this.ruleForm.workAddress,
                jobType: this.ruleForm.jobType,
            }
            editOrAddJobInfoApi(params).then(res=>{
                this.$message({
                    message: res.data.message,
                    type: 'success',
                    duration: 800,
                })
                setTimeout(() => {
                    this.show = false
                }, 800);
            })
          } 
        });
      },

      resetForm(formName) {
          this.ruleForm = {};
      },
      close(){
        //   $emit('update:show', false);
          this.$router.go(0);
      },

        // 判断是否为管理员
        isAdminFunc(){
            const params = {
                account: getStore("account"),
            }
            isAdminApi(params).then(res=>{
                this.isAdmin = res.data.data;
            })
        },

    },
    created(){

        // 判断是否为管理员
       this.isAdminFunc();

    }
}
</script>

<style scoped>

</style>
